package main

import (
	"flag"
	"fmt"
	"fox/config"
	"fox/db"
	"fox/jsonrpc"
	"fox/middleware"
	"fox/routers"
	"fox/validators"
	"github.com/gin-gonic/gin"
)

var (
	confFile string
)

func initArgs() {
	flag.StringVar(&confFile, "config", "./config/config.json", "指定config.json文件")
	flag.Parse()
}

func main() {
	var (
		r   *gin.Engine
		err error
	)

	//初始化命令行
	initArgs()

	r = gin.New()

	r.Use(gin.Logger(), gin.Recovery())
	r.Use(middleware.CoresMiddleware())

	//加载自定义规则验证
	validators.InitValidateRegister()

	//加载配置
	if err = config.InitConfig(confFile); err != nil {
		fmt.Println("加载配置文件错误:", err)
		panic(err)
	}

	//初始化数据库
	if err = db.InitDB(); err != nil {
		fmt.Println("加载数据库错误:", err)
		panic(err)
	}

	//加载路由
	routers.InitRouters(r)

	//初始化jsonrpc路由
	if err = jsonrpc.InitConfig(); err != nil {
		panic(err)
	}

	//静态资源
	r.Static("/assets", "./assets")

	if err = r.Run(config.GConfig.Ip + config.GConfig.Port); err != nil {
		panic(err)
	}

}
